<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/17 0017
 * Time: 上午 11:41
 */
namespace Org\Apply\InstallAndUninstall;

use Org\Apply\InstallAndUninstall\BaseInterface as Base;

abstract class Apply implements Base{

    public $app_path;//当前项目路径

    function __construct()
    {
        $this->app_path = dirname($_SERVER['SCRIPT_FILENAME']) . '/'; // 当前项目路径
    }

    /**
     * 安装对应文件
     * @param 应用名称 $applyName
     * @return int
     */
    public function install($applyName)
    {
        // TODO: Implement install() method.
        $xiarikui_version = file_get_contents($this->app_path . 'Application/Admin/Conf/version.txt'); // XiaRiKui 版本
        $config = include $this->app_path . 'Public/application/' . strtolower($applyName) . '/config.php'; // 当前应用适合哪些版本
        $config['version'] = explode(',', $config['version']);
        if (!in_array($xiarikui_version, $config['version'])) {
            $info['status'] = 0;
            $info['msg'] = '版本不兼容';
            return $info;
        }
        // 复制文件
        recurse_copy($this->app_path . 'Public/application/' . strtolower($applyName) . '/www/', $this->app_path);
        return $info['status'] = 1;
    }

    /**
     * 安装对应数据表及新增数据表
     * @param 应用名称 $applyName
     * @return array
     */
    public function install_sql($applyName)
    {
        // TODO: Implement install_sql() method.
        $sql = file($this->app_path . 'Public/application/' . strtolower($applyName) . '/install.sql');

        $sql = $this->formMatSql($sql);
        return $sql;
    }

    /**
     * 删除对应文件
     * @param 应用名称 $applyName
     */
    public function uninstall($applyName)
    {
        // TODO: Implement uninstall() method.
        file_exists($this->app_path . 'Application/Admin/Controller/' . $applyName . 'Controller.class.php') ?
            delFile($this->app_path . 'Application/Admin/Controller/' . $applyName . 'Controller.class.php') : '' ;
        file_exists($this->app_path . 'Application/Admin/View/' . $applyName) ?
            delFile($this->app_path . 'Application/Admin/View/' . $applyName) && rmdir($this->app_path . 'Application/Admin/View/' . $applyName) : '' ;

        file_exists($this->app_path . 'Application/App/Controller/' . $applyName . 'Controller.class.php') ?
            delFile($this->app_path . 'Application/App/Controller/' . $applyName . 'Controller.class.php') : '' ;

        file_exists($this->app_path . 'Application/Home/Controller/' . $applyName . 'Controller.class.php') ?
            delFile($this->app_path . 'Application/Home/Controller/' . $applyName . 'Controller.class.php') : '' ;

        file_exists($this->app_path . 'Application/Mobile/Controller/' . $applyName . 'Controller.class.php') ?
            delFile($this->app_path . 'Application/Mobile/Controller/' . $applyName . 'Controller.class.php') : '' ;

        file_exists($this->app_path . 'Tpl/Home/default' . $applyName) ?
            delFile($this->app_path . 'Tpl/Home/default' . $applyName) && rmdir($this->app_path . 'Tpl/Home/default/' . $applyName) : '' ;
        file_exists($this->app_path . 'Tpl/Home/fresh' . $applyName) ?
            delFile($this->app_path . 'Tpl/Home/fresh' . $applyName) && rmdir($this->app_path . 'Tpl/Home/fresh/' . $applyName) : '' ;

        file_exists($this->app_path . 'Tpl/Home/default' . $applyName) ?
            delFile($this->app_path . 'Tpl/Home/default' . $applyName) && rmdir($this->app_path . 'Tpl/Mobile/default/' . $applyName) : '' ;
        file_exists($this->app_path . 'Tpl/Mobile/fresh' . $applyName) ?
            delFile($this->app_path . 'Tpl/Mobile/fresh' . $applyName) && rmdir($this->app_path . 'Tpl/Mobile/fresh/' . $applyName) : '' ;

    }

    /**
     * 执行更改数据表操作
     * @param 应用名称 $applyName
     * @return array
     */
    public function uninstall_sql($applyName)
    {
        // TODO: Implement uninstall_sql() method.
        $sql = file($this->app_path . 'Public/application/' . strtolower($applyName) . '/uninstall.sql');
        $sql = $this->formMatSql($sql);
        return $sql;
    }

    /**
     * 格式化sql代码
     * @param $sql
     * @return array
     */
    private function formMatSql($sql)
    {
        $sqlstr="";
        foreach($sql as $line){
            $line=trim($line);
            if($line!=""){
                if(!($line{0}=="#" || $line{0}.$line{1}=="--")){
                    $sqlstr.=$line;
                }
            }
        }
        $sqlstr=rtrim($sqlstr,";");
        $sqls=explode(";",$sqlstr);
        return $sqls;
    }

    /**
     * 添加目录
     * @param $applyName
     */
    protected function addMenu($applyName)
    {
//        $attr = [
//            'distribut' => array('name' => '分销管理', 'icon' => 'fa-cubes', 'sub_menu' => array(
//                array('name' => '分销关系', 'act' => 'index', 'control' => 'AppDistribut'),
//                array('name' => '分销设置', 'act' => 'set', 'control' => 'AppDistribut'),
//                array('name' => '提现列表', 'act' => 'cash', 'control' => 'AppDistribut'),
//                array('name' => '分成列表', 'act' => 'bonus', 'control' => 'AppDistribut'),
//                array('name' => '汇款列表', 'act' => 'record', 'control' => 'AppDistribut'),
//            ))
//        ];
//        $attr = ['appdistribut'=>array('name' => '分销管理', 'icon' => 'fa-cubes', 'sub_menu' => array(
//            array('name' => '分销关系', 'act' => 'index', 'control' => 'AppDistribut'),
//            array('name' => '分销设置', 'act' => 'set', 'control' => 'AppDistribut'),
////                array('name' => '申请列表', 'act' => 'apply', 'control' => 'AppDistribut'),
//            array('name' => '提现列表', 'act' => 'cash', 'control' => 'AppDistribut'),
//            array('name' => '分成列表', 'act' => 'bonus', 'control' => 'AppDistribut'),
//            array('name' => '汇款列表', 'act' => 'record', 'control' => 'AppDistribut'),
//        ))];
//        $attr = json_encode($attr);
//        file_put_contents($this->app_path . 'Public/application/' . strtolower($applyName) . '/' . $applyName . '_menu.txt', $attr);
//        die();
        $menu = file_get_contents(APP_PATH . 'Admin/Conf/menu.txt');
        $menu = json_decode($menu, true);
        if(strlen($menu[$applyName]) === 0 || empty($menu[$applyName])) {
            $newMenu = file($this->app_path . 'Public/application/' . strtolower($applyName) . '/' . $applyName . '_menu.txt');
            $newMenu = json_decode($newMenu[0], true);
            $subMenu = array_merge($menu['application']['sub_menu'], $newMenu);
            $menu['application']['sub_menu'] = $subMenu;
            file_put_contents(APP_PATH . 'Admin/Conf/menu.txt', json_encode($menu));
        }
    }

    /**
     * 删除目录
     * @param $applyName
     */
    protected function removeMenu($applyName) {
        $menu = file_get_contents(APP_PATH . 'Admin/Conf/menu.txt');
        $menu = json_decode($menu, true);
        if(isset($menu['application']['sub_menu'][$applyName])) {
            unset($menu['application']['sub_menu'][$applyName]);
        }
        file_put_contents(APP_PATH . 'Admin/Conf/menu.txt', json_encode($menu));
    }


}